﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HROReporting.Services.Interfaces;
using HROReporting.Model.DocentIncheckingen;
using HROReporting.Model.NFCDevices;
using HROReporting.Infrastructure.UnitOfWork;
using HROReporting.Model.PresentieIncheckingen;
using HROReporting.Services.Messaging.PresentieIncheckingService;
using HROReporting.Model.Docenten;

namespace HROReporting.Services.Implementations
{
    public class PresentieIncheckingService : IPresentieIncheckingService
    {
        private readonly IPresentieIncheckingRepository _presentieIncheckingRepository;
        private readonly IDocentIncheckingRepository _docentIncheckingRepository;
        private readonly IUnitOfWork _uow;

        public PresentieIncheckingService(IPresentieIncheckingRepository presentieIncheckingRepository,
                                          IDocentIncheckingRepository docentIncheckingRepository,
                                          IUnitOfWork uow)
        {
            _presentieIncheckingRepository = presentieIncheckingRepository;
            _docentIncheckingRepository = docentIncheckingRepository;
            _uow = uow;
        }
        
        public Boolean CheckIfStudentHasCheckidIn(string studentNumber)
        {
            return _presentieIncheckingRepository.CheckIfStudentCheckingIn(studentNumber);
        }

        public void CreateInchecking(CreatePresentieIncheckingRequest request)
        {
            int now = Convert.ToInt32(DateTime.Now.TimeOfDay.ToString().Replace(":", "").Remove(6));
            foreach (Presentielijst presentielijst in request.Presentielijsten)
            {
                if (presentielijst.Datum.Date == DateTime.Now.Date)
                {
                    
                    if (now < Convert.ToInt32(presentielijst.LesuurEind.StartTijd.Replace(":", "")))
                    {
                        PresentieInchecking inchecking = new PresentieInchecking();
                        inchecking.PresentielijstId = presentielijst.Id;
                        inchecking.Studentnummer = request.Studentnummer;
                        inchecking.Datum = DateTime.Now;

                        _presentieIncheckingRepository.Add(inchecking);
                        _uow.Commit();

                        break;
                    }
                }
            }
        }

        public GetPresentielijstenBySerialResponse GetPresentielijstenBySerial(GetPresentielijstenBySerialRequest request)
        {
            GetPresentielijstenBySerialResponse response = new GetPresentielijstenBySerialResponse();

            IEnumerable<Presentielijst> presentielijsten = _docentIncheckingRepository.GetPresentielijstenBySerial(request.serial);

            response.Presentielijsten = presentielijsten;

            return response;
        }

        public GetLesUrenResponse GetAllLesUren()
        {
            throw new NotImplementedException();
        }
    }
}
